package com.magic.smartcity.view.more.hospital

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RadioGroup
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.Navigation
import com.google.android.material.snackbar.Snackbar
import com.google.android.material.textfield.TextInputLayout
import com.magic.smartcity.R
import com.magic.smartcity.databinding.FragmentHospitalPatientCardAddBinding
import com.magic.smartcity.view.more.viewmodels.HospitalViewModel

class HospitalPatientCardAddFragment : Fragment() {
    private lateinit var binding: FragmentHospitalPatientCardAddBinding

    private val hospViewModel by lazy {
        ViewModelProvider(this).get(HospitalViewModel::class.java)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentHospitalPatientCardAddBinding.inflate(layoutInflater)

        initView()

        return binding.root
    }

    private fun initView() {

        //性别单选按钮设置数据改动
        var sex= "0"
        binding.sexGroup.setOnCheckedChangeListener { _, checkedId ->
            sex = if (binding.nan.isChecked) "0" else "1"
        }
        binding.submitAdd.setOnClickListener {
            //检查是否存在空输入
            when {
                binding.name.text.toString().isEmpty() -> {
                    binding.name.error = "请输入姓名"
                }
                binding.idCard.text.toString().isEmpty() -> {
                    binding.idCard.error = "请输入身份证号"
                }
                binding.phonenumber.text.toString().isEmpty() -> {
                    binding.phonenumber.error = "请输入手机号"
                }
                binding.address.text.toString().isEmpty() -> {
                    binding.address.error = "请输入地址"
                }
                binding.birthday.text.toString().isEmpty() -> {
                    binding.birthday.error = "请输入生日日期"
                }
                //没有存在空输入，检查输入格式
                else -> {
                    val regex = """[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}""".toRegex()
                    when {
                        //检查日期格式是否输入正确
                        !regex.matches(binding.birthday.text.toString()) -> {
                            binding.birthday.error = "格式错误!年-月-日"
                        }
                        //所有检查通过添加就诊人信息
                        else -> {
                            hospViewModel.addHospitalPatinentCard(
                                binding.name.text.toString(),
                                binding.address.text.toString(),
                                binding.birthday.text.toString(),
                                binding.phonenumber.text.toString(),
                                binding.idCard.text.toString(),
                                sex
                            )
                            hospViewModel.addHospitalPatientCardOk.observe(viewLifecycleOwner){
                                Toast.makeText(context, "新增成功", Toast.LENGTH_SHORT).show()
                                Navigation.findNavController(binding.root).popBackStack()
                            }
                        }
                    }
                }
            }
        }


    }

}